<script setup>
import { reactive, ref } from 'vue';

import { User, Lock, Key } from '@element-plus/icons-vue'

import { ElMessage } from 'element-plus'

import { useRouter } from 'vue-router'

import { adminStore, tokenStore } from '../store';

// 引入axios编写api
import adminApi from '../api/adminApi.js'
import captchaApi from '../api/captchaApi.js';

const admin = reactive({
  phone: '13220653961',
  password: '1234',
  captcha: '',
  key:''
});

// 获取路由对象
const router = useRouter();

// 验证码图片数据
const captcha = ref("");

// 加载验证码
loadCaptcha();

function onSubmit(){
  console.log(admin)
  // ajax异步请求，调用服务端   axios
  adminApi.login(admin).then(result=>{
    console.log("登录信息",result)
    if (result.code === 200){
      // 登录用户的信息保存到Pinia中
      adminStore().update(result.data);
      // 将Token保存到Pinia中
      tokenStore().update(result.msg);
      ElMessage.success("登录成功")
      // 登录成功了， 跳转路由
      router.push("/charts");
    } else {
      ElMessage({
        message: result.msg,
        type: 'error',
        offset: 250,
      });
      loadCaptcha();
    }
  });

}

// 加载验证码
function loadCaptcha(){
  console.log('获取验证码')
  captchaApi.get().then(result=>{
    admin.key = result.data.msg; // key
    captcha.value = result.data.data;  // 图片
    console.log(result.data)
  })
}

</script>


<template>
  <div class="login-bg">
    <el-form class="form-login" :model="admin">
      <el-row>
        <el-col :span="10">
          <img style="width: 100px" src="@/assets/logo.png" alt="中享思途" />
        </el-col>
        <el-col :span="14">
          <h1>系统登录</h1>
        </el-col>
      </el-row>
      <el-form-item>
        <el-input v-model="admin.phone"
                  placeholder="请输入手机号"
                  :prefix-icon="User"
                  maxlength="16"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="admin.password"
                  placeholder="请输入密码"
                  show-password
                  :prefix-icon="Lock"
                  maxlength="16"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="admin.captcha"
                  placeholder="验证码"
                  :prefix-icon="Key"
                  maxlength="5"
                  class="input-verCode"></el-input>
        <img :src="captcha"
             class="img-verCode" @click="loadCaptcha">
      </el-form-item>
      <el-form-item>
        <el-button class="btn-login" type="success" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

.login-bg{
  height: 100%;
  background-image: url('../assets/bg.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
}

.form-login{
  width: 280px;
  padding: 20px;
  position: absolute;
  top: 20%;
  left: calc(50% - 150px);
  background-color: #FFF;
  box-shadow: 10px 10px 30px #000;
}

.btn-login{
  width: 100%;
}
h1{
  text-align: center;
  font-size: 24px;
  margin-bottom: 18px;
  height: 50px;
  line-height: 50px;
}
.input-verCode{
  width: 160px;
}
.img-verCode{
  width: 80px;
  height:30px;
}
</style>